﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Data;

public partial class ShopTaskFinishRate : ExtBaseRpt
{
    mrjapp mrj = new mrjapp();
    string mid;
    protected void Page_Load(object sender, EventArgs e)
    {
        mid = Request.Params["mid"];
        if (!X.IsAjaxRequest)
        {
            try
            {
                InsActLog(mid, "浏览");
                txtBigAreaName.GetStore().DataSource = mydb.DM_GetDateSet("SELECT DISTINCT ProvinceName AS BigAreaName FROM t_task_shop_permissions "); 
                txtAreaName.GetStore().DataSource = mydb.DM_GetDateSet("SELECT DISTINCT AreaName FROM t_task_shop_permissions ");
                txtDisplay.GetStore().DataSource = mydb.DM_GetDateSet("SELECT DISTINCT Display FROM t_task_shop_permissions WHERE ShopState<>'已撤店' AND Display IS NOT NULL ");
            }
            catch (Exception ex)
            {
                ShowBox("错误：" + ex.Message);
            }
        }
        if (Session["UserID"] == null)
        {
            ShowBox("用户登入超时请求重新登入");
        }
    }

    protected void GetArea(object sender, DirectEventArgs e)
    {
        txtAreaName.GetStore().DataSource = mydb.DM_GetDateSet("SELECT DISTINCT AreaName FROM t_task_shop_permissions WHERE ProvinceName='" + txtBigAreaName.Value + "'");
        this.Store_AreaName.DataBind();
    }

    protected void RefreshDataSet(object sender, StoreReadDataEventArgs e)
    {
        BtnQureyUser(null, null);
    }

    protected void StartDateField(object sender, DirectEventArgs e)
    {
        this.txtProcessingTimeEnd.MinDate = Convert.ToDateTime(this.txtProcessingTimeBegin.Text);
        int Begin = Convert.ToInt32(Convert.ToDateTime(this.txtProcessingTimeBegin.Text).DayOfWeek);
        if (Begin == 0)
        {
            this.txtProcessingTimeEnd.MaxDate = Convert.ToDateTime(this.txtProcessingTimeBegin.Text);
            this.txtProcessingTimeEnd.Text = this.txtProcessingTimeBegin.Text;
        }
        else
        {
            this.txtProcessingTimeEnd.MaxDate = Convert.ToDateTime(this.txtProcessingTimeBegin.Text).AddDays(7 - Begin);
            if (Convert.ToDateTime(this.txtProcessingTimeEnd.Text) < Convert.ToDateTime(this.txtProcessingTimeBegin.Text))
            {
                this.txtProcessingTimeEnd.Text = this.txtProcessingTimeBegin.Text;
            }
            else if(Convert.ToDateTime(this.txtProcessingTimeEnd.Text) > Convert.ToDateTime(this.txtProcessingTimeBegin.Text).AddDays(7 - Begin))
            {
                this.txtProcessingTimeEnd.Text = Convert.ToDateTime(this.txtProcessingTimeBegin.Text).AddDays(7 - Begin).ToString();
            }
        }
        
    }

    protected void BtnQureyUser(object sender, DirectEventArgs e)
    {
        try
        {
            string sql = string.Empty;
            string strBigAreaName = this.txtBigAreaName.Text;
            string strAreaName = this.txtAreaName.Text;
            string strShopName = this.txtShopName.Text.Trim();
            string strDisplay = this.txtDisplay.Text;
            string txtProcessingTimeBegin = this.txtProcessingTimeBegin.Text;
            string txtProcessingTimeEnd = this.txtProcessingTimeEnd.Text;
            if (string.IsNullOrEmpty(txtProcessingTimeBegin) || Convert.ToDateTime(txtProcessingTimeBegin) == Convert.ToDateTime("0001-01-01 00:00:00"))
            {
                ShowBox("请输入开始日期条件！");
                return;
            }
            if (string.IsNullOrEmpty(txtProcessingTimeEnd) || Convert.ToDateTime(txtProcessingTimeEnd) == Convert.ToDateTime("0001-01-01 00:00:00"))
            {
                ShowBox("请输入结束日期条件！");
                return;
            }

            txtProcessingTimeBegin = Convert.ToDateTime(txtProcessingTimeBegin).ToString("yyyy-MM-dd");
            txtProcessingTimeEnd = Convert.ToDateTime(txtProcessingTimeEnd).ToString("yyyy-MM-dd");

            string strTab = string.Empty;
            

            sql += string.Format(@"SELECT 
	Num1.TaskNum,
	Num1.ShopID,
	Num1.ShopCode,
	TSP.ShopName,
	(TSP.ProvinceName + '/' + TSP.AreaName) AS ZoneAddress,
	TSP.OperatorAreaName,
	TSP.Display,
	CASE WHEN IsNull(Num2.TaskNum,0)=0 THEN 0 ELSE 1 END AS IsEqual
FROM 
(
	SELECT COUNT(Num.ShopID) AS TaskNum,Num.ShopID,Num.ShopCode FROM 
	(
		SELECT DISTINCT b.TaskID,USDR.ShopID,USDR.ShopCode FROM t_task_detail_finish AS a
		INNER JOIN t_task_shoplist AS b
		ON a.TaskID=b.TaskID 
		AND a.ProcessingTime>='{0}'  
		AND a.ProcessingTime<='{1} 23:59:59'
		INNER JOIN  [V_UserShopDataRights] AS USDR 
		ON a.ProcessingCreator=USDR.userid
	) AS Num
	INNER JOIN t_task_set AS c
	ON Num.TaskID=c.TaskID AND c.IsMustFinish=1 AND c.IsEnabled=1
	GROUP BY Num.ShopID,Num.ShopCode
) AS Num1
INNER JOIN t_task_shop_permissions AS TSP
ON Num1.ShopCode=TSP.ShopCode
LEFT JOIN
(
SELECT COUNT(TSL.ShopID) AS TaskNum,TSL.ShopID FROM t_task_shoplist AS TSL
INNER JOIN t_task_set AS TTS
ON TSl.TaskID=TTS.TaskID AND TTS.IsMustFinish=1 AND TTS.IsEnabled=1
GROUP BY TSL.ShopID
) AS Num2
ON Num2.ShopID=Num1.ShopID AND Num2.TaskNum=Num1.TaskNum 
WHERE 1=1 
AND TSP.ShopState<>'已撤店' ", txtProcessingTimeBegin, txtProcessingTimeEnd);
            
            if (!string.IsNullOrEmpty(strBigAreaName))
            {
                sql += string.Format(@" AND TSP.ProvinceName='{0}' ", strBigAreaName);
            }
            if (!string.IsNullOrEmpty(strAreaName))
            {
                sql += string.Format(@" AND TSP.AreaName='{0}' ", strAreaName);
            }
            if (!string.IsNullOrEmpty(strShopName))
            {
                sql += string.Format(@" AND (TSP.ShopName LIKE '%{0}%' OR Num1.ShopID='{1}') ", strShopName, strShopName);
            }
            if (!string.IsNullOrEmpty(strDisplay))
            {
                sql += string.Format(@" AND TSP.Display='{0}' ", strDisplay);
            }

            this.Store1.DataSource = mydb.DM_GetDateSet(sql);
            this.Store1.DataBind();
        }
        catch (Exception ex)
        {
            ShowBox("查询出错：" + ex.Message);
        }
    }
}